package com.ruoyi.question.service.impl;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.question.domain.EQuestionCategorization;
import com.ruoyi.question.mapper.EQuestionCategorizationMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.question.mapper.EQuestionMapper;
import com.ruoyi.question.domain.EQuestion;
import com.ruoyi.question.service.IEQuestionService;

/**
 * 问题列表Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-26
 */
@Service
@Slf4j
public class EQuestionServiceImpl extends ServiceImpl<EQuestionMapper,EQuestion> implements IEQuestionService
{
    @Autowired
    private EQuestionMapper eQuestionMapper;

    @Autowired
    private EQuestionCategorizationMapper eQuestionCategorizationMapper;

    @Autowired
    private EQuestionCategorizationServiceImpl eQuestionCategorizationService;

    /**
     * 查询问题列表
     *
     * @param id 问题列表主键
     * @return 问题列表
     */
    @Override
    public EQuestion selectEQuestionById(Long id)
    {
        return eQuestionMapper.selectEQuestionById(id);
    }

    /**
     * 查询问题列表列表
     *
     * @param eQuestion 问题列表
     * @return 问题列表
     */
    @Override
    public List<EQuestion> selectEQuestionList(EQuestion eQuestion)
    {
        return eQuestionMapper.selectEQuestionList(eQuestion);
    }

    /**
     * 新增问题列表
     *
     * @param eQuestion 问题列表
     * @return 结果
     */
    @Override
    public int insertEQuestion(EQuestion eQuestion)
    {
        eQuestion.setCreateTime(DateUtils.getNowDate());
        eQuestion.setQuestionStatus(Long.valueOf(0));
        EQuestionCategorization eQuestionCategorization=eQuestionCategorizationMapper.selectEQuestionCategorizationById(eQuestion.getCid());
        eQuestionCategorization.setReferencedStatus(Long.valueOf(1));
        eQuestionCategorization.setLastModifiedTime(new Date());
        eQuestionCategorizationMapper.updateEQuestionCategorization(eQuestionCategorization);
        return eQuestionMapper.insertEQuestion(eQuestion);
    }

    /**
     * 修改问题列表
     *
     * @param eQuestion 问题列表
     * @return 结果
     */
    @Override
    public int updateEQuestion(EQuestion eQuestion)
    {

        eQuestion.setLastModifiedTime(new Date());
        EQuestionCategorization eQuestionCategorization=eQuestionCategorizationMapper.selectEQuestionCategorizationById(eQuestion.getCid());
        eQuestionCategorization.setReferencedStatus(Long.valueOf(1));
        eQuestionCategorization.setLastModifiedTime(new Date());
        eQuestionCategorizationMapper.updateEQuestionCategorization(eQuestionCategorization);
        return eQuestionMapper.updateEQuestion(eQuestion);
    }

    /**
     * 批量删除问题列表
     *
     * @param ids 需要删除的问题列表主键
     * @return 结果
     */
    @Override
    public int deleteEQuestionByIds(Long[] ids)
    {
        for (Long id : ids) {
            eQuestionCategorizationService.updateCategorizationReferenceStatus(id);
        }
        return eQuestionMapper.deleteEQuestionByIds(ids);
    }

    /**
     * 删除问题列表信息
     *
     * @param id 问题列表主键
     * @return 结果
     */
    @Override
    public int deleteEQuestionById(Long id)
    {
        eQuestionCategorizationService.updateCategorizationReferenceStatus(id);
        return eQuestionMapper.deleteEQuestionById(id);
    }

    /*
    * 修改问题的状态
    * */
    @Override
    public int updateEQuestionStatus(EQuestion eQuestion) {
        if(eQuestion.getQuestionStatus()==0){
            eQuestion.setQuestionStatus(Long.valueOf(1));
        }else{
            eQuestion.setQuestionStatus(Long.valueOf(0));
        }
        return eQuestionMapper.updateEQuestion(eQuestion);
    }
}
